﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CRMWeb.Utilities
{
    public static class Extensions
    {
        public static string ToStringDbNull(this object obj)
        {
            return ToStringDbNull(obj, String.Empty);
        }

        public static string ToStringDbNull(this object obj, string defValue)
        {
            return obj != DBNull.Value && obj != null ? obj.ToString() : defValue;
        }

        public static DateTime ToDateTime(this object obj, DateTime defaultDateTime)
        {
            try
            {
                if (obj != null && obj != DBNull.Value)
                {
                    return Convert.ToDateTime(obj.ToString());
                }

                return defaultDateTime;
            }
            catch
            {
                return defaultDateTime;
            }
        }

        public static DateTime? ToDateTime(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToDateTime(obj) : null as DateTime?;
            }
            catch
            {
                return null;
            }
        }

        public static TimeSpan? ToTimeSpan(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? (TimeSpan?)obj : null as TimeSpan?;
            }
            catch
            {
                return null;
            }
        }

        public static bool? ToBool(this object obj)
        {
            try
            {
                return ToBool(obj, false);
            }
            catch
            {
                return null;
            }
        }

        public static bool? ToBool(this object obj, bool defaultValue)
        {
            try
            {
                bool value;

                if (obj != DBNull.Value && obj != null && bool.TryParse(obj.ToString(), out value))
                {
                    return value;
                }

                return defaultValue;
            }
            catch
            {
                return defaultValue;
            }
        }

        public static int? ToInt(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToInt32(obj) : (int?)null;
            }
            catch
            {
                return null;
            }
        }

        public static short? ToShort(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToSByte(obj) : (short?)null;
            }
            catch
            {
                return null;
            }
        }

        public static long? ToLong(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToInt64(obj) : 0;
            }
            catch
            {
                return null;
            }
        }

        public static decimal? ToDecimal(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToDecimal(obj) : (decimal?)null;
            }
            catch
            {
                return null;
            }
        }

        public static Guid ToGuid(this object obj)
        {
            return obj != DBNull.Value && obj != null ? new Guid(obj.ToString()) : new Guid();
        }

        public static List<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
        {
            return listToClone.Select(item => (T)item.Clone()).ToList();
        }

        public static double? ToDouble(this object obj)
        {
            try
            {
                return obj != DBNull.Value && obj != null ? Convert.ToDouble(obj) : (double?)null;
            }
            catch
            {
                return null;
            }
        }

        public static DateTime FirstDayOfMonth(this DateTime date)
        {
            return date.AddDays((date.Day - 1) * -1);
        }

        public static DateTime LastDayOfMonth(this DateTime date)
        {
            return date.AddDays((date.Day - 1) * -1).AddMonths(1).AddDays(-1);
        }

        public static string JsEscape(this object obj)
        {
            var s= "\"";
            return obj.ToString().Replace("'","\\'").Replace(s,"\\'") ;
        }


        
        #region Validation
        public static bool IsValidEmailAddress(this string s)
        {
            //Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            Regex regex = new Regex(@"\w+([-+.']\w+)*@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }

        public static bool IsValidPhoneNumber(this string s)
        {
            Regex regex = new Regex(@"^[0-9]+$",
                                    RegexOptions.IgnorePatternWhitespace);

            return regex.IsMatch(s);
        }

        #endregion

    }
}
